const {
  http
} = require("../../../../utils/apiutils");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jobData: {},
    userInfo: {},
    latitude: "",
    longitude: "",
    checked: true,
    timeArr: [],
    times: [],
    show: false,
    interviewDate: "",
    interviewDates: "",
  },
  onChange: function () {
    this.setData({
      checked: !this.data.checked
    })
  },
  onclickMS: function () {
    this.setData({
      show: true,
    })
  },
  selectTime: function (e) {
    console.log("--------", e);
    this.setData({
      interviewDate: e.detail.value,
      interviewDates: e.detail.name,
    })
  },
  onClose () {
    this.setData({
      show: false,
    })
  },

  baoming(cancelConflict) {
    console.log(this)
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var data = {
      lng: this.data.longitude, //经度信息
      lat: this.data.latitude, //维度信息
    };
    if (getApp().globalData.inviteCode != "") {
      data["inviteCode"] = getApp().globalData.inviteCode;
    }
    if (this.data.jobData.postType == 2) {
      data["interviewDate"] = this.data.interviewDate;
    }
    if (cancelConflict) {
      data["cancelConflict"] = true;
    }
    console.log("------提交报名信息------", data);
    http.registration(this.data.jobData.id, data,(res=>{
        wx.hideLoading()
        if (res.code == 0) {
          console.log(that)
          var  type = 1;
          if(this.data.jobData.type==2)
          {
              type = 2;
          } 
          let _this  =this;
          wx.navigateBack({
            delta: 2,
            success: function () {
              // http.sendMsg(`${_this.data.jobData.factoryId}/${getApp().globalData.userInfo.id}`, () => {}, () => {})
              wx.navigateTo({
                url: '/pages/me/pages/action/action?type=1',
              })
              try {
                http.sendMsg(`${_this.data.jobData.factoryId}/${_this.data.jobData.id}/${getApp().globalData.userInfo.id}`, () => {}, () => {})
                getApp().globalData.inviteCode="";
              } catch (error) {
                console.log(error)
              }
            }
          })
        } else if (res.code == 3) {
          var that = this;
          wx.showModal({
            title: "提示",
            content: "当前报名岗位与已录用岗位工作时间冲突,是否取消原报名岗位继续报名",
            success: function (res) {
              if (res.confirm) {
                that.baoming(true)
              }
            }
          })
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      }).bind(this),
      function (err) {
        wx.hideLoading()
      })
  },

  add0(m) {
    return m < 10 ? '0' + m : m
  },


  weekToStr: function (week) {
    var str = "";
    if (week == 0) {
      str = "周日";
    } else if (week == 1) {
      str = "周一";
    } else if (week == 2) {
      str = "周二";
    } else if (week == 3) {
      str = "周三";
    } else if (week == 4) {
      str = "周四";
    } else if (week == 5) {
      str = "周五";
    } else if (week == 6) {
      str = "周六";
    }
    return str;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      jobData: getApp().globalData.workInfo,
      userInfo: getApp().globalData.userInfo,
    })
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        console.log(res);
        that.data.latitude = res.latitude;
        that.data.longitude = res.longitude;
      },
    })
    //今天的时间
    // var day2 = new Date();
    // day2.setTime(day2.getTime());
    // var s2 = day2.getFullYear() + "-" + this.add0(day2.getMonth() + 1) + "-" + this.add0(day2.getDate());
    // var w2 = this.weekToStr(day2.getDay());

    // var day3 = new Date();
    // day3.setTime(day3.getTime() + 24 * 60 * 60 * 1000);
    // var s3 = day3.getFullYear() + "-" + this.add0(day3.getMonth() + 1) + "-" + this.add0(day3.getDate());
    // var w3 = this.weekToStr(day3.getDay());

    // var day4 = new Date();
    // day4.setTime(day4.getTime() + 24 * 60 * 60 * 1000 * 2);
    // var s4 = day4.getFullYear() + "-" + this.add0(day4.getMonth() + 1) + "-" + this.add0(day4.getDate());
    // var w4 = this.weekToStr(day4.getDay());

    // var day5 = new Date();
    // day5.setTime(day5.getTime() + 24 * 60 * 60 * 1000 * 3);
    // var s5 = day5.getFullYear() + "-" + this.add0(day5.getMonth() + 1) + "-" + this.add0(day5.getDate());
    // var w5 = this.weekToStr(day5.getDay());

    // console.log("----------", s2, s3, s4, s5);

    // var day6 = new Date();
    // day6.setTime(day5.getTime() + 24 * 60 * 60 * 1000 * 4);
    // var s6 = day6.getFullYear() + "-" + this.add0(day6.getMonth() + 1) + "-" + this.add0(day6.getDate());
    // console.log("----------", s2, s3, s4, s5);
    // var w6 = this.weekToStr(day6.getDay());

    // var day7 = new Date();
    // day7.setTime(day5.getTime() + 24 * 60 * 60 * 1000 * 3);
    // var s7 = day5.getFullYear() + "-" + this.add0(day7.getMonth() + 1) + "-" + this.add0(day7.getDate());
    // var w7 = this.weekToStr(day7.getDay());
    var arr = []
    // var str = this.data.jobData.interviewDate;

    var num = 24 * 60 * 60 * 1000
    console.log("-------处理前的数组----", arr, this.data.jobData.interviewDate);
    var str1 = this.data.jobData.interviewDate;
    for (var i = 0; i < 20; i++) {
      var day = new Date();
      day.setTime(day.getTime() + num * i);
      var s = day.getFullYear() + "-" + this.add0(day.getMonth() + 1) + "-" + this.add0(day.getDate());
      var w = this.weekToStr(day.getDay());
      console.log("----------", str1, w);
      if (str1.indexOf(w) != -1) {
        if (i == 0) {
          var data = {
            name: "今天(" + s + ")",
            value: s
          }
          arr.push(data)
        } else if (i == 1) {
          var data = {
            name: "明天(" + s + ")",
            value: s
          }
          arr.push(data)
        } else {
          var data = {
            name: s,
            value: s
          }
          arr.push(data)
        }
      }
      if (arr.length == 4) {
        break;
      }
    }
    this.setData({
      timeArr: arr,
    })
    console.log("-------处理后的数组----", arr);
    // this.setData({
    //   timeArr: [{
    //       name: "今天(" + s2 + ")",
    //       value: s2
    //     }, {
    //       name: "明天(" + s3 + ")",
    //       value: s3
    //     }, {
    //       name: "后天(" + s4 + ")",
    //       value: s4
    //     },
    //     {
    //       name: s5,
    //       value: s5
    //     }
    //   ]
    // })
    // timeArr = ["今天","明天","后天",]
    console.log("--------------", this.data.times);
  },

  onclickNext () {

    if (!this.data.checked) {
      wx.showToast({
        title: '您需要同意工作服务条款才能报名工作',
        icon: "none",
      })
      return;
	}
	

    if (this.data.jobData.postType == 2) {
      if (this.data.interviewDate == "") {
        this.setData({
          show: true,
        })
        return
      }
  }
  
	// 缴纳 押金
	if(this.data.jobData.isMargin==1){
		// 必须缴纳押金
		this.payYj();
	}else if(getApp().globalData.userInfo.workfx){
		// 个人存在风险，需缴纳押金
		this.payYj();
  }
  },
  payYj() {
    let _this  =this;
		http.payMargin({
				jobId: this.data.jobData.id
			}, function (res) {
        console.log(111,res)
        wx.requestPayment(
          {
          "appId":res.data.appid,
          "timeStamp": res.data.timestamp,
          "nonceStr": res.data.noncestr,
          "package": res.data.package,
          "signType": "MD5",
          "paySign": res.data.sign,
          "success":function(res1){
            _this.baoming(false);
          },
          "fail":function(fail){
            console.log(fail)
            wx.showToast({
              title: '支付失败',
              icon:"none"
            })
          },
          "complete":function(res){}
          })
				
				
			},
			function (err) {
				wx.hideLoading()
			})
	},
  serve: function () {
    wx.navigateTo({
      url: '/pages/home/pages/service/service',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var shareObj = {
      title: "分享岗位", // 默认是小程序的名称(可以写slogan等)
      path: getApp().globalData.userInfo.inviteCode?'/pages/index/index?code='+getApp().globalData.userInfo.inviteCode:'/pages/index/index', // 
      imageUrl: '',
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
        }
      }
    }
    console.log("------------", shareObj);
    // 返回shareObj
    return shareObj;
  }
})